<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>插入排序</title>
</head>
<body>
<h3>插入排序</h3>
<script>
	// function insertionSort(arr){
	// 	var len = arr.length;
	// 	var preIndex,current;
	// 	for(var i =1;i<len;i++){
	// 		preIndex = i - 1; 0  1
	// 		current = arr[i]; 2  15
	// 		while(preIndex >=0 && arr[preIndex] > current){ 2 3 15 8 9
	// 			arr[preIndex +1] =arr[preIndex]; arr[1] = 3
	// 			preIndex--;
	// 		}
	// 		arr[preIndex+1] =current  arr[0] = 2
	// 	}
	// 	return arr;
	// }
	// console.log( insertionSort([1,3,2,3,5]) )
    function insertSort(arr){
        let len = arr.length
        let preIndex, current ;
        for(let i = 1; i< len ; i++){
            preIndex = i - 1
            current = arr[i]
            while(preIndex>=0 && arr[preIndex] > current){
                arr[preIndex + 1] = arr[preIndex]
                preIndex--
            }
            arr[preIndex + 1] = current
        }
        return arr
    }

    console.log(insertSort([5,74,5,6,47,8,4,65,52,12]))
</script>
</body>
</html>
